#!/usr/bin/python3
#!-*- conding:UTF-8 -*-
"""
@file            get-rpm.py
@author          mening
@date            2024-03-18 14:13:20
@copyright       Copyright (c) 2024 by 北京长扬科技有限公司, ALL Rights Reserved.
@details         由于生成dump的虚拟机不方便挂载本地iso镜像，该脚本仅将本地镜像中合适架构的rpm包拿到。
"""
import os
import glob
import subprocess

rpm_tar_name='centos-7-1'
cmd_path = os.getcwd()
work_path = '/home/mening-test/centos7-x'
print("[debug]:work_path is %s" %work_path)
mount_path = work_path + '/cdrom'
rpm_directory = mount_path + '/Packages/'
tmp_dir = work_path + '/tmp/'

if not os.path.exists('/dev/cdrom'):
    print('[mount]:not find iso...')

print('[mount]:find iso!')

if not os.path.exists(mount_path):
    print('[check-dir]:' + mount_path + ' not found, create it.')
    os.system('mkdir -p ' + mount_path)

os.system('mount /dev/cdrom ' + mount_path)
rpm_x86_64_list = glob.glob(os.path.join(rpm_directory,'*.x86_64.rpm'))
rpm_noarch_list = glob.glob(os.path.join(rpm_directory,'*.noarch.rpm'))

cnt =0
if not os.path.exists(tmp_dir):
    print('[check-dir]:' + tmp_dir + ' not found, create it.')
    os.system('mkdir -p ' + tmp_dir)
for rpm_file in rpm_x86_64_list:
    os.system('cp -rfp ' + rpm_file + ' ' + tmp_dir )
    cnt += 1

for rpm_file in rpm_noarch_list:
    os.system('cp -rfp ' + rpm_file + ' ' + tmp_dir )
    cnt += 1
print("[get-rpm-list]:get rpm package: %d" %cnt)

os.system('cd ' + work_path)
print('[tar]:tar -zcf ' + rpm_tar_name + '.tar.gz ' + tmp_dir + '*.rpm')
os.system('tar -zcf ' + rpm_tar_name + '.tar.gz ' + tmp_dir + '*.rpm')
